<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
          { name: 'box-base', title: '基础信息' },
          { name: 'box-info', title: '字典数据' },
        ]"
      />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem label="识别码" name="code" :parent="_self" :form="form" />
          <customFormItem label="字典名称" name="name" :parent="_self" :form="form" />
          <customFormItem label="描述" name="description" :parent="_self" :form="form" full-width />

          <div id="box-info" class="box-title">字典数据</div>
          <t-row v-show="strings.isNotBlank(form.id)" style="width: 100%">
            <data-view
              v-show="propStatus === 'edit'"
              propViewKey="sys_dict_data"
              propViewForm="sys/dict-data/detail.vue"
              :propInitData="form"
              ref="childDataView"
              :propHidePageSearch="false"
              :propAutoFetchData="false"
              :propCustomCols="[{ name: 'color', cmode: 'edit' }]"
              @before-load="beforeLoad"
              :propIsTdesign="true"
            >
              <template #color="{ field, value }">
                <div :style="`height: 20px;border:1px solid;background-color: ${value}`" />
              </template>
            </data-view>
          </t-row>
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
      viewData: {},
    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        this.viewData = this.propCurrentRow
        // 注意：如果childDataView通过v-if进行判断是否显示，此处代码不会执行
        if (this.form.id) {
          this.$refs.childDataView.setQueryWrapper(this.$newWrapper().eq('dictTypeId', this.form.id))
        } else {
          this.$refs.childDataView.setQueryWrapper(this.$newWrapper().ne('1', 1))
        }
      },
      afterSave: () => {},
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {},
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {},
      beforeClose: () => {
        return true
      },
      afterClick() {},
    })
  },
  props: [
    'propPrimaryKey',
    'propDictionaries',
    'propCurrentRow',
    'propViewFormData',
    'propReadonly',
    'propStatus',
    'propRules',
    'propInitData',
  ],
  components: { customAnchor },
  computed: {},
  methods: {
    beforeLoad() {
      // 通用参数-字典类型可编辑
      if (!this.parameters.dictEditConfigIsEnabled(this)) {
        this.$nextTick(() => {
          // 表格按钮隐藏
          this.$refs.childDataView.setButtonInvisible('view', 'vb-add')
          this.$refs.childDataView.setButtonInvisible('view', 'vb-delete')
          // 行按钮隐藏
          this.$refs.childDataView.setButtonInvisible('row', 'vrb-edit')
          this.$refs.childDataView.setButtonInvisible('row', 'vrb-delete')
          this.$refs.childDataView.setButtonInvisible('row', 'vrb-view', false)
          // 详情页按钮隐藏
          this.$refs.childDataView.setButtonInvisible('viewForm', 'vfb-add')
          this.$refs.childDataView.setButtonInvisible('viewForm', 'vfb-save')
          this.$refs.childDataView.setButtonInvisible('viewForm', 'vfb-delete')
        })
      } else {
        this.$nextTick(() => {
          this.$refs.childDataView.setButtonInvisible('row', 'vrb-view')
        })
      }
    },
  },
}
</script>
